package com.etc.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.rowset.CachedRowSet;

import com.etc.dao.IDeptDao;
import com.etc.entity.Dept;
import com.etc.entity.Employees;
import com.etc.util.DBUtil;

public class DeptDaoImpl implements IDeptDao {
	
	/**
	 * 根据部门id查部门信息
	 * deptId:部门id
	 * return: 部门对象
	 */
	@Override
	public Dept queryById(int deptId) {
		CachedRowSet crs = DBUtil.executeQuery("select * from dept where deptId=?", deptId);
		Dept dept =null;
		try {
			while(crs.next()) {
				String deptName = crs.getString(2);
				dept = new Dept(deptId, deptName);		
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dept;
	}
	/**
	 * 查询所有部门
	 * return:部门集合
	 */
	@Override
	public List<Dept> queryDept() {
		Dept dept=new Dept();
		List<Dept> list =new  ArrayList<Dept>();
		CachedRowSet rs = DBUtil.executeQuery("select * from dept");
		try {
			while(rs.next()) {
				Integer deptId = rs.getInt("deptId");
				String deptName = rs.getString("deptName");				
				dept = new Dept(deptId, deptName);
				list.add(dept);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	@Override
	public Dept queryDeptByName(String deptName) {
		CachedRowSet crs = DBUtil.executeQuery("select * from dept where deptName=?", deptName);
		Dept dept =null;
		try {
			if(crs.next()) {
				dept = new Dept(crs.getInt(1), crs.getString(2));		
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return dept;
	}

	

}
